package rm.metrology.shared;

import java.io.Serializable;

import javax.persistence.Id;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import com.googlecode.objectify.annotation.Cached;
import com.googlecode.objectify.annotation.Unindexed;

@Cached
public class CostEntity implements Serializable {

	
	private static final long serialVersionUID = 1L;

	@Id 
	private Long fId;
	
	@Unindexed 
	private Long fCreatedAt = System.currentTimeMillis();
	
	@Unindexed 
	@NotNull(message="[Staff] is REQUIRED")
	private String fStaff;
	
	@Unindexed
	@NotNull(message="[Calibration Type] is REQUIRED")
	private String fCalibType;
	
	@Unindexed
	@NotNull(message="[Range Start] is REQUIRED")
	@Size(min=0, max=10000, message="[Range Start] between 0 and 10000")
	private Float fRangeStart;
	
	@Unindexed
	@NotNull(message="[Range End] is REQUIRED")
	@Size(min=0, max=10000, message="[Range End] between 0 and 10000")
	private Float fRangeEnd;
	
	@Unindexed
	@NotNull(message="[Cost] is REQUIRED")
	@Size(min=0, max=1000, message="[Cost] between 0 and 1000")
	private Integer fCost;
	
	
	protected CostEntity() {
	}
	
	public CostEntity(String staff, CalibrationEnum calibType,
			Float rangeStart, Float rangeEnd, Integer cost) {
		fStaff = staff;
		fCalibType = calibType.toString();
		fRangeStart = rangeStart;
		fRangeEnd = rangeEnd;
		fCost = cost;
	}	
	
	public final Long getId() {
		return fId;
	}
	
	public final Long getCreatedTime() {
		return fCreatedAt;
	}

	public final String getStaff() {
		return fStaff;
	}

	public final CalibrationEnum getCalibType() {
		return CalibrationEnum.toEnum(fCalibType);
	}

	public final Float getRangeStart() {
		return fRangeStart;
	}

	public final Float getRangeEnd() {
		return fRangeEnd;
	}

	public final Integer getCost() {
		return fCost;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof MetroEntity) {
			MetroEntity metro = (MetroEntity) obj;
			if (fCalibType.equals(metro.getCalibType())) {
				Float max = metro.getEquipMax();
				return (max >= fRangeStart) && (max < fRangeEnd);
			}
		} 
		return false;
	}
	
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append(getClass().getName())
			   .append(" { Staff: \"").append(fStaff)
			   .append("\", CreatedAt: ").append(fCreatedAt)
			   .append(", CalibType: \"").append(fCalibType)
			   .append("\", RangeStart: ").append(fRangeStart)
			   .append(", RangeEnd: ").append(fRangeEnd)
			   .append(", Cost: ").append(fCost)
			   .append(" }");
		return builder.toString();
	}

}
